<?php

/**
 * Created by PhpStorm.
 * User: zc
 * Date: 2018/5/14
 * Time: 17:20
 */

namespace app\modules\api\models;

use app\hejiang\ApiResponse;
use app\hejiang\BaseApiResponse;
use app\models\IntegralGoods;
use app\models\IntegralOrderDetail;
use app\models\IntegralSetting;
use app\models\Model;
use app\models\MsOrder;
use app\models\Order;
use app\models\Register;
use app\modules\api\models\ApiModel;
use app\models\Banner;
use app\models\Coupon;
use app\models\PurchaseMoneyLog;

use app\models\User;
use yii\data\Pagination;

class PurchaseMoneyLogForm extends ApiModel
{
    public $user_id;
    public $store_id;
    public $limit;
    public $page;

    public function rules()
    {
        return [
            [['page','user_id','store_id', 'limit'], 'integer'],
            [['limit'], 'default', 'value' => 10],
        ];
    }

 
 public function search()
    {
		
        if (!$this->validate())
            return $this->errorResponse;
		
			$query = PurchaseMoneyLog::find()->alias('g')->where([
            'g.user_id' => $this->user_id,
            'g.store_id' => $this->store_id,
        ]);	
		$pagination = new Pagination(['totalCount' => $count, 'pageSize' => $this->limit, 'page' => $this->page - 1]);

            //综合，自定义排序+时间最新
        $query->orderBy('g.id DESC');
		$list = $query
            ->leftJoin(['o' => 'hjmall_order'], 'o.id=g.order_id')
            ->select('g.*,o.order_no')
            ->limit($pagination->limit)
            ->offset($pagination->offset)
            ->asArray()->groupBy('g.id')->all();
		foreach($list as $key=>$val){
			$list[$key]['addtime'] = date('Y-m-d H:i',$val['addtime']);
		}
		
        $data = [
            'list' => $list,
			'row_count' => $count,
            'page_count' => $pagination->pageCount
			
        ];
	
        return new ApiResponse(0, 'success', $data);
    }
   

  
}
